package com.collectplus.express.logic;

import com.collectplus.express.ExpressApplication;
import com.collectplus.express.model.AppConfigBean;
import com.collectplus.express.model.IdCardInfoBean;
import com.collectplus.express.model.LocationBean;
import com.collectplus.express.model.OrderBean;
import com.collectplus.express.model.ReceiverBean;
import com.collectplus.express.model.SameCityParcelBean;
import com.collectplus.express.model.SenderBean;
import com.collectplus.express.model.SerachStationBean;
import com.collectplus.express.model.UserBean;
import com.collectplus.express.wxapi.Constants;

import java.io.File;
import java.util.ArrayList;

import droid.frame.Config;
import droid.frame.utils.lang.ObjectSerialize;

/**
 * 项目中用到的数据存储接口<br>
 * Cache 是一组接口<br>
 * {@link ObjectSerialize} 是具体的存储方式<br>
 * 不推荐直接使用ObjectSerialize类来存储, 因为具体的存储方式可能会改变
 *
 * @author coffee<br>
 * 2015-6-18下午4:54:51
 */
public class AppCache extends droid.frame.utils.lang.Cache {

    private static AppConfigBean appConfig;
    /**
     * 当前的订单Id
     */
    private static int orderId;

    @Deprecated
    private static OrderBean order;

    //
    private static int userId;
    private static int parcelId;
    // 代收使用的id
    private static int shbUserId;
    private static UserBean user;
    // 商户号id
    private static String outTradeNo;

    //当前定位城市
    private static String currentCityName;

    /**
     * 当前正在创建的包裹
     */
    private static ReceiverBean parcelNew;

    /**
     * 上门位置
     */
    private static LocationBean location;

    /**
     * 身份认证信息
     */
    private static IdCardInfoBean cardInfoBean;
    /**
     * 同城直送缓存信息
     */
    private static SameCityParcelBean sameCityParcelBean;

    /**
     * 选择投递点历史搜索集合
     */
    private static SerachStationBean selfSerachStationBean;

    /**
     * 单独的保存寄件人的信息
     */
    private static SenderBean senderBean;

    public static int getParcelId() {
        return parcelId;
    }

    public static void setParcelId(int parcelId) {
        AppCache.parcelId = parcelId;
    }

    public static LocationBean getLocation() {
        return location;
    }

    public static void setLocation(LocationBean location) {
        AppCache.location = location;
    }

    /**
     * 判断是否已经登录
     *
     * @return
     */
    public static boolean isLogin() {
        return getUserId() != 0;
    }

    public static int getUserId() {
        if (user == null) {
            user = getUser();
        }
        if (user != null) {
            userId = user.getId();
        }
        return userId;
    }

    public static void setUser(UserBean user) {
        AppCache.user = user;
        if (user != null) {
            userId = user.getId();
            //			write(user);
            ExpressApplication.getCurrAccount().setObject(Constants.SHB_USER_INFO, user);
        } else {
            userId = 0;
            shbUserId = 0;
            //            remove(UserBean.class);
            ExpressApplication.getCurrAccount().setObject(Constants.SHB_USER_INFO, null);
        }
    }

    public static void setUserName(String username) {
        AppCache.user = getUser();
        user.setName(username);
        //        write(user);
        ExpressApplication.getCurrAccount().setObject(Constants.SHB_USER_INFO, user);
    }

    public static UserBean getUser() {
        if (user == null) {
            //            user = get(UserBean.class);
            user = ExpressApplication.getCurrAccount().getObject(Constants.SHB_USER_INFO, UserBean.class);
        }
        return user;
    }

    // /
    public static int getOrderId() {
        if (orderId == 0) {
            OrderBean order = getOrder();
            if (order != null) {
                orderId = order.getId();
            }
        }
        return orderId;
    }

    public static void setOrderId(int orderId) {
        AppCache.orderId = orderId;
    }

    public static String getOutTradeNo() {
        return AppCache.outTradeNo;
    }

    public static void setOutTradeNo(String outTradeNo) {
        AppCache.outTradeNo = outTradeNo;
    }

    public static OrderBean getOrder() {
        if (order == null) {
            order = ObjectSerialize.read(OrderBean.class, null);
        }
        return order;
    }

    public static void setOrder(OrderBean order) {
        AppCache.order = order;
        if (order == null) {
            AppCache.remove(OrderBean.class);
            AppCache.setOrderId(0);
            AppCache.setParcelId(0);
        } else {
            setParcelId(order.getParcelId());
            setOrderId(order.getId());
            asyncWrite(order, null);
        }
    }

    /**
     * 删除所有订单相关的数据
     */
    public static void removeOrderAlls() {
        setOrder(null);// 清空订单
        AppConfigBean config = getAppConfig();
        //
        if (config != null) {
            config.setOrderStatus(0);
        }
    }

    /**
     * app配置信息 ---------------------------------
     *
     * @param config
     */
    public static void setAppConfig(AppConfigBean config) {
        AppCache.appConfig = config;
        if (config == null) {
            AppCache.remove(AppConfigBean.class);
        } else {
            asyncWrite(config, null);
        }
    }

    public static AppConfigBean getAppConfig() {
        return AppCache.appConfig;
    }

    public static void clearAppMsgNum() {
        if (appConfig != null) {
            appConfig.setMessageNum(0);
        }
    }

    /**
     * 退出登录执行的操作, 清空存储卡的缓存
     */
    public final static void onLogout() {
        // 清图片缓存
        File cacheDir = new File(Config.getCacheDir());
        cacheDir.delete();
        // 清拍照缓存
        File captureDir = new File(Config.getCaptureDir());
        captureDir.delete();
        // 清空上门地址信息
        AppCache.setLocationItems(null);
        // 清空订单信息
        AppCache.removeOrderAlls();
        setUser(null);
        //
        // SharedPref.clearData();
    }

    /**
     * 新增位置信息 ---------------------------------
     */
    public static void addLocation(LocationBean location) {
        asyncAppend(location);
    }

    @SuppressWarnings("unchecked")
    public static ArrayList<LocationBean> getLocations() {
        return ObjectSerialize.read(ArrayList.class, LocationBean.class);
    }

    public static void setLocationItems(ArrayList<LocationBean> items) {
        if (items == null) {
            ObjectSerialize.remove(ArrayList.class, LocationBean.class);
        } else {
            asyncWrite(items, LocationBean.class);
        }
    }

    public static void setParcelNew(ReceiverBean parcelNew) {
        AppCache.parcelNew = parcelNew;
        if (parcelNew == null) {
            ObjectSerialize.remove(ReceiverBean.class, null);
        } else {
            asyncWrite(parcelNew, ReceiverBean.class);
        }
    }

    public static ReceiverBean getParcelNew() {
        if (parcelNew == null) {
            parcelNew = ObjectSerialize.read(ReceiverBean.class, null);
        }
        return parcelNew;
    }

    public static String getCurrentCityName() {
        return currentCityName;
    }

    public static void setCurrentCityName(String currentCityName) {
        AppCache.currentCityName = currentCityName;
    }

    public static IdCardInfoBean getCardInfoBean() {
        if (cardInfoBean == null) {
            cardInfoBean = ObjectSerialize.read(IdCardInfoBean.class, null);
        }
        return cardInfoBean;
    }

    public static void setCardInfoBean(IdCardInfoBean cardInfoBean) {
        AppCache.cardInfoBean = cardInfoBean;
        if (cardInfoBean == null) {
            AppCache.remove(IdCardInfoBean.class);
        } else {
            asyncWrite(cardInfoBean, null);
        }
    }

    public static SameCityParcelBean getSameCityParcelBean() {
        if (sameCityParcelBean == null) {
            sameCityParcelBean = ObjectSerialize.read(SameCityParcelBean.class, null);
        }
        return sameCityParcelBean;
    }

    public static void setSameCityParcelBean(SameCityParcelBean sameCityParcelBean) {
        AppCache.sameCityParcelBean = sameCityParcelBean;
        if (sameCityParcelBean == null) {
            ObjectSerialize.remove(SameCityParcelBean.class, null);
        } else {
            asyncWrite(sameCityParcelBean, SameCityParcelBean.class);
        }
    }

    public static SerachStationBean getSelfSerachStationBean(String userId) {
        if (selfSerachStationBean == null) {
//            selfSerachStationBean = ObjectSerialize.read(SerachStationBean.class, null);
            selfSerachStationBean = ExpressApplication.getCurrAccount().getObject(userId, SerachStationBean.class);
        }
        return selfSerachStationBean;
    }

    public static void setSelfSerachStationBean(String userId, SerachStationBean selfSerachStationBean) {
        AppCache.selfSerachStationBean = selfSerachStationBean;
        if (selfSerachStationBean == null) {
//            ObjectSerialize.remove(SerachStationBean.class, null);
            ExpressApplication.getCurrAccount().setObject(userId, null);
        } else {
            ExpressApplication.getCurrAccount().setObject(userId, selfSerachStationBean);
//            asyncWrite(selfSerachStationBean, SerachStationBean.class);
        }
    }

    public static SenderBean getSenderBean() {
        if (user == null) {
            return null;
        } else {
            if (senderBean == null) {
                senderBean = ExpressApplication.getCurrAccount().getObject(user.getPhone(), SenderBean.class);
            }
            return senderBean;
        }
    }

    public static void setSenderBean(SenderBean senderBean) {
        if (user != null) {
            AppCache.senderBean = senderBean;
            if (senderBean != null) {
                ExpressApplication.getCurrAccount().setObject(user.getPhone(), senderBean);
            } else {
                ExpressApplication.getCurrAccount().setObject(user.getPhone(), null);
            }
        }
    }
}
